﻿#include "VHistoricalRecordsList.h"
#include "VFunctions.h"
#include "VGlobal.h"
#include <QFileInfo>

VHistoricalRecordsList::VHistoricalRecordsList(QWidget *parent)
	: QListWidget(parent)
{
	connect(this, &VHistoricalRecordsList::itemClicked, this, &VHistoricalRecordsList::onItemClicked);
}

VHistoricalRecordsList::~VHistoricalRecordsList()
{
}

void VHistoricalRecordsList::load()
{
	QListWidgetItem* item;
	QString text = VFunctions::read_file_text(VGlobal::historical_record_path);
	QStringList list = text.split("\r\n");
	foreach(QString path, list)
	{
		if (path.length() == 0)
		{
			continue;
		}

		if (QFile(path).exists() == false)
		{
			continue;
		}

		item = new QListWidgetItem(this);
		item->setText(QString("项目:%1 \r\n路径:%2").arg(QFileInfo(path).fileName()).arg(path));
		item->setIcon(QIcon(VGlobal::icon_path.value("project")));
		item->setData(0, QVariant::fromValue(path));
	}
}

void VHistoricalRecordsList::onItemClicked(QListWidgetItem* item)
{
	QString path = item->data(0).value<QString>();
	emit signalOpenProject(path);
}
